package com.argrace.smart.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationSet;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.argrace.smart.R;


/**
 * dialog.setTitleText("标题", ArgraceDialog.TITLE_MID)
 * 
 * dialog.setContextText() dialog.setCancelText() dialog.setConfirmText()
 * dialog.setEditHintText() dialog.showCancelButton() dialog.setBtnColor()
 * 
 * @author Elune1943
 * 
 */
public class ArgraceDialog extends Dialog implements View.OnClickListener {
	// /** 有标题无内容两个按钮 */
	// public static final int TYPE_HAS_TITLE_NO_CONTENT_TWO_BUTTON = 0;// 有标题
	// /** 无标题大内容两个按钮 */
	// public static final int TYPE_NO_TITLE_HAS_BIG_CONTENT_TWO_BUTTON = 1;
	// /** 有标题有编辑框两个按钮 */
	// public static final int TYPE_HAS_TITLE_HAS_EDIT_TWO_BUTTON = 2;// 有标题
	// /** 普通更新、普通内容、两个按钮 */
	// public static final int TYPE_NEW_VERSION_NORMAL_CONTENT_TOW_BUTTON = 3;
	// /** 强制更新、普通内容、1个按钮 */
	// public static final int TYPE_NEW_VERSION_NORMAL_CONTENT_ONE_BUTTON = 4;
	// /** 无标题、普通内容、1个按钮 */
	// public static final int TYPE_NO_TITLE_NORMAL_CONTENT_ONE_BUTTON = 5;
	// /** 无标题、普通内容、2个按钮 */
	// public static final int TYPE_NO_TITLE_NORMAL_CONTENT_TWO_BUTTON = 6;
	// /** 有标题、普通内容、2个按钮 */
	// public static final int TYPE_HAS_TITLE_NORMAL_CONTENT_TWO_BUTTON = 7;//
	// 有标题
	/** 一个按钮 */
	// public static final int TYPE_ONE_BUTTON = 1;
	// /** 2个按钮 */
	// public static final int TYPE_TWO_BUTTON = 2;

	public static final int TITLE_LEFT = 1;// 标题center_vertical靠左
	public static final int TITLE_MID = 2;// 标题bottom居中
	public static final int TITLE_IMG = 3;// 带图片的标题
	public static final int TITLE_NO = 4;// 无标题

	public static final int CONTENT_BIG = 1;// 居中大文字
	public static final int CONTENT_NORMAL = 2;// 居中普通文字
	public static final int CONTENT_EDIT = 3;// 编辑框
	public static final int CONTENT_NO = 4;// 无内容

	private AnimationSet mModalInAnim;
	private OnArgraceDialogClickListener mCancelClickListener;
	private OnArgraceDialogClickListener mConfirmClickListener;

	private View mDialogView;
	private Button mConfirmButton;
	private Button mCancelButton;
	private LinearLayout mWarmLay, mLeftTitleLay, mCenterTitleLay;
	private LinearLayout mBigContentLay, mNormalContentLay, mEditContentLay;
	private View mFirstLine, mSecondLine, mThirdLine;
	private TextView mWarmText, mLeftTitleText, mCenterTitleText,
			mBigContentText, mNormalContentText;
	private EditText mEditText;
	private String mTitleStr, mContentStr, mConfirmStr, mCancelStr, mEditHint;
	private int mLeftColor, mRightColor;
	private ImageView mImg;
	private boolean mCloseFromCancel;
	private boolean mShowCancel;
	private int titleType;// 1 左 2 中 3带图片
	private int contentType;// 1普通类容 2 大内容 3 编辑框
	private Context context;
	private int mInputType = 1;

	public static interface OnArgraceDialogClickListener {
		public void onClick(ArgraceDialog sweetAlertDialog);
	}

	public ArgraceDialog(Context context) {
		this(context, TITLE_LEFT);
	}

	public ArgraceDialog(Context context, int alertType) {
		super(context, R.style.alert_dialog);
		this.context = context;
		setCancelable(true);
		setCanceledOnTouchOutside(false);
		mModalInAnim = (AnimationSet) OptAnimationLoader.loadAnimation(
				getContext(), R.anim.modal_in);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout._alert_dialog);
		mDialogView = getWindow().getDecorView().findViewById(
				android.R.id.content);
		mConfirmButton = (Button) findViewById(R.id.ad_confirm_button);
		mCancelButton = (Button) findViewById(R.id.ad_cancel_button);
		mImg = (ImageView) findViewById(R.id.ad_custom_image);
		mWarmLay = (LinearLayout) findViewById(R.id.ad_warm_lay);
		mLeftTitleLay = (LinearLayout) findViewById(R.id.ad_left_title_lay);
		mCenterTitleLay = (LinearLayout) findViewById(R.id.ad_center_title_lay);
		mBigContentLay = (LinearLayout) findViewById(R.id.ad_center_big_content_lay);
		mNormalContentLay = (LinearLayout) findViewById(R.id.ad_normal_content_lay);
		mEditContentLay = (LinearLayout) findViewById(R.id.ad_edit_lay);
		mFirstLine = findViewById(R.id.ad_first_line);
		mSecondLine = findViewById(R.id.ad_second_line);
		mThirdLine = findViewById(R.id.ad_third_line);
		mWarmText = (TextView) findViewById(R.id.ad_warm_text);
		mLeftTitleText = (TextView) findViewById(R.id.ad_left_title_text);
		mCenterTitleText = (TextView) findViewById(R.id.ad_center_title_text);
		mBigContentText = (TextView) findViewById(R.id.ad_center_big_content_text);
		mNormalContentText = (TextView) findViewById(R.id.ad_normal_content_text);
		mEditText = (EditText) findViewById(R.id.ad_edit_text);

		mConfirmButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);

		restore();
		setContextText(mContentStr, contentType);
		setTitleText(mTitleStr, titleType);
		setEditHintText(mEditHint);
		setCancelText(mCancelStr, mShowCancel);
		setConfirmText(mConfirmStr);
		setBtnColor(mLeftColor, mRightColor);
		setEditTextStyle(R.drawable.border_corners_orange, mInputType);
	}

	/**
	 * TitleType ：1 左 2 中 3带图片
	 * 
	 * @param text
	 * @return
	 */
	public ArgraceDialog setTitleText(String text, int type) {
		mTitleStr = text;
		titleType = type;

		if (titleType == 1) {
			if (mLeftTitleText != null && mTitleStr != null) {
				mLeftTitleText.setText(mTitleStr);
				mLeftTitleLay.setVisibility(View.VISIBLE);
				mFirstLine.setVisibility(View.VISIBLE);
			}

		} else if (titleType == 2) {
			if (mCenterTitleText != null && mTitleStr != null) {
				mCenterTitleText.setText(mTitleStr);
				mCenterTitleLay.setVisibility(View.VISIBLE);
				mFirstLine.setVisibility(View.GONE);
			}
		} else if (titleType == 3) {
			if (mWarmText != null && mTitleStr != null) {
				mWarmText.setText(mTitleStr);
				mWarmLay.setVisibility(View.VISIBLE);
				mFirstLine.setVisibility(View.VISIBLE);
			}
		} else {// noTitle

		}

		return this;
	}

	/**
	 * 
	 * @param text
	 * @param type
	 * @return
	 */
	public ArgraceDialog setContextText(String text, int type) {
		mContentStr = text;
		contentType = type;

		if (contentType == 1) {
			if (mBigContentText != null && mContentStr != null) {
				mBigContentText.setText(mContentStr);
				mBigContentLay.setVisibility(View.VISIBLE);
				mSecondLine.setVisibility(View.VISIBLE);
			}
		} else if (contentType == 2) {
			if (mNormalContentText != null && mContentStr != null) {
				mNormalContentText.setText(mContentStr);
				mNormalContentLay.setVisibility(View.VISIBLE);
				mSecondLine.setVisibility(View.VISIBLE);
			}
		} else if (contentType == 3) {
			if (mEditText != null && mContentStr != null) {
				mEditContentLay.setVisibility(View.VISIBLE);
				mSecondLine.setVisibility(View.VISIBLE);
			}
		} else {// noContent

		}

		return this;
	}

	public String getEditText() {
		return mEditText.getText().toString();
	}

	protected void onStart() {
		mDialogView.startAnimation(mModalInAnim);
	}

	public String getConfirmText() {
		return mConfirmStr;
	}

	public ArgraceDialog setBtnColor(int leftColor, int rightColor) {
		mLeftColor = leftColor;
		mRightColor = rightColor;
		// if (mConfirmButton != null && mLeftColor != null) {
		// mConfirmButton.setTextColor(Color.parseColor(leftColor));
		// }
		// if (mCancelButton != null && mRightColor != null) {
		// mCancelButton.setTextColor(Color.parseColor(rightColor));
		// }

		if (mCancelButton != null && mLeftColor > 0) {
			mCancelButton.setTextColor(context.getResources().getColor(
					mLeftColor));
		}
		if (mConfirmButton != null && mRightColor > 0) {
			mConfirmButton.setTextColor(context.getResources().getColor(
					mRightColor));
		}

		return this;
	}

	public ArgraceDialog setConfirmText(String text) {
		mConfirmStr = text;
		if (mConfirmButton != null && mConfirmStr != null) {
			mConfirmButton.setText(mConfirmStr);
		}
		return this;
	}

	public ArgraceDialog setEditHintText(String text) {
		mEditHint = text;

		if (mEditText != null && mEditHint != null) {
			mEditText.setHint(mEditHint);
		}

		return this;
	}

	public String getCancelText() {
		return mCancelStr;
	}

	public ArgraceDialog setCancelText(String text, boolean isShow) {
		mShowCancel = isShow;
		mCancelStr = text;
		if (mCancelButton != null) {
			showCancelButton(mShowCancel);
			if (mCancelStr != null) {
				mCancelButton.setText(mCancelStr);
			} else {
				mCancelButton.setText("取消");
			}

		}
		return this;
	}

	private void dismissWithAnimation(boolean fromCancel) {
		mCloseFromCancel = fromCancel;
		// mConfirmButton.startAnimation(mOverlayOutAnim);
		if (mCloseFromCancel) {
			ArgraceDialog.super.cancel();
		} else {
			ArgraceDialog.super.dismiss();
		}
	}

	public boolean isShowCancelButton() {
		return mShowCancel;
	}

	/**
	 * 是否显示取消键
	 * 
	 * @param isShow
	 * @return
	 */
	private void showCancelButton(boolean isShow) {
		if (mCancelButton != null) {
			mCancelButton.setVisibility(isShow ? View.VISIBLE : View.GONE);

			if (!isShow) {
				mThirdLine.setVisibility(View.GONE);
				mConfirmButton.setBackgroundResource(R.drawable.dialog_btn_btm);
			}
		}
	}

	private void restore() {
		mImg.setVisibility(View.GONE);
		mWarmLay.setVisibility(View.GONE);
		mLeftTitleLay.setVisibility(View.GONE);
		mCenterTitleLay.setVisibility(View.GONE);
		mBigContentLay.setVisibility(View.GONE);
		mNormalContentLay.setVisibility(View.GONE);
		mEditContentLay.setVisibility(View.GONE);
		mFirstLine.setVisibility(View.GONE);
		mSecondLine.setVisibility(View.GONE);

	}

	// private void changeAlertType(int alertType) {
	// mAlertType = alertType;
	// // call after created views
	// if (mDialogView != null) {
	// // if (!fromCreate) {
	// // restore all of views state before switching alert type
	//
	// // }
	// switch (mAlertType) {
	// /* 1个按钮 */
	// case TYPE_ONE_BUTTON:
	// mFirstLine.setVisibility(View.VISIBLE);
	// mThirdLine.setVisibility(View.GONE);
	// mCancelButton.setVisibility(View.GONE);
	// break;
	// /* 两个按钮 */
	// case TYPE_TWO_BUTTON:
	// mBigContentLay.setVisibility(View.VISIBLE);
	// mFirstLine.setVisibility(View.VISIBLE);
	//
	// break;
	//
	// default:
	// break;
	// }
	// // if (!fromCreate) {
	// // playAnimation();
	// // }
	// }
	// }

	@Override
	public void cancel() {
		dismissWithAnimation(true);
	}

	public void dismissWithAnimation() {
		dismissWithAnimation(false);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.ad_cancel_button) {
			if (mCancelClickListener != null) {
				mCancelClickListener.onClick(ArgraceDialog.this);
			} else {
				dismissWithAnimation();
			}
		} else if (v.getId() == R.id.ad_confirm_button) {
			if (mConfirmClickListener != null) {
				mConfirmClickListener.onClick(ArgraceDialog.this);
			} else {
				dismissWithAnimation();
			}
		}
	}

	public ArgraceDialog setCancelClickListener(
			OnArgraceDialogClickListener listener) {
		mCancelClickListener = listener;
		return this;
	}

	public ArgraceDialog setConfirmClickListener(
			OnArgraceDialogClickListener listener) {
		mConfirmClickListener = listener;
		return this;
	}

	public ArgraceDialog setEditTextStyle(int res_background, int Type) {
		mInputType = Type;
		Log.i("info", "editText是" + mEditText);
		if (mEditText != null) {
			mEditText.setBackgroundResource(res_background);
			mEditText.setInputType(mInputType);

		}
		return this;
	}

}
